/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.magallanes.utilities;

import com.rw.magallanes.core.Warehouse;
import com.rw.magallanes.db.Storage;
import com.rw.magallanes.db.StorageException;
import java.awt.Window;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.JOptionPane;
import javax.swing.KeyStroke;
import javax.swing.UIManager;

/**
 *
 * @author Gonzalez
 */
public class Utilities {

    private static SimpleDateFormat sdf = new SimpleDateFormat("ddMMyyyyHHmmssSSSS");

    public static void loadLookAndFeel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        } catch (Exception ex) {
            try {
                UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
            } catch (Exception ex1) {
                throw new RuntimeException("No se pudo cargar L&F por defecto");
            }
        }
    }

    public static Warehouse loadWarehouse() throws StorageException {
        File storageDir = new File(Storage.STORAGE_DIR);
        if (!storageDir.exists()) {
            storageDir.mkdir();
            Storage.getInstance().loadDefault();
        } else {
            File storageFile = new File(storageDir, Storage.STORAGE_FILE);
            if (!storageFile.exists()) {
                Storage.getInstance().loadDefault();
            } else {
                Storage.getInstance().load();
            }
        }
        return Storage.getInstance().getWarehouse();
    }

    public static boolean showYesNo(Window window, String msg) {
        int option = JOptionPane.showConfirmDialog(window, msg, "Aviso", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
        return option == JOptionPane.YES_OPTION;
    }

    public static int showYesNoCancel(Window window, String msg) {
        return JOptionPane.showConfirmDialog(window, msg, "Aviso", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.WARNING_MESSAGE);
    }

    public static void showMsg(Window window, String msg, MessageType type) {
        JOptionPane.showMessageDialog(window, msg, type.getTitle(), type.getOptionType());
    }

    public static void printWarehouse(Warehouse wh) {
    }

    public static String name(String pattern) {
        Date date = new Date();
        return pattern + sdf.format(date);
    }

    public static void configureAction(JComponent component, Action action) {
        InputMap inputMap = component.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
        ActionMap actionMap = component.getActionMap();
        KeyStroke keyStroke = (KeyStroke) action.getValue(Action.ACCELERATOR_KEY);
        String actionCommand = (String) action.getValue(Action.ACTION_COMMAND_KEY);
        inputMap.put(keyStroke, actionCommand);
        actionMap.put(actionCommand, action);
    }
}
